#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "The format is as follows:"
    echo "bash zlib-1.12.11.sh buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
fi

[[ ! "$1" =~ ^/.* || ! -d "$1" || "$1" = "/" ]] &&  echo "请输入正确的构建路径" && exit 1
[[ ! "$2" =~ ^/.* || ! -d "$2" || "$2" = "/" ]] &&  echo "请输入正确的安装路径" && exit 1

buildpath=$1
installpath=$2
[[ "$buildpath" =~ .*/$ ]] && buildpath=${buildpath%/*}
[[ "$installpath" =~ .*/$ ]] && installpath=${installpath%/*}
[[ "$buildpath" == "$installpath" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
set -e
yum install -y wget tar environment-modules

if `wget -t 40 -c -P $buildpath  https://ghproxy.com/https://github.com/broadinstitute/gatk/releases/download/4.0.0.0/gatk-4.0.0.0.zip`
then 
   echo "gatk-4.0.0.0.zip下载成功!(国内github加速下载)"
elif `wget -t 40 -c -P $buildpath  https://github.com/broadinstitute/gatk/releases/download/4.0.0.0/gatk-4.0.0.0.zip`
then 
   echo "gatk-4.0.0.0.zip下载成功!"
else
   echo "gatk-4.0.0.0.zip下载失败!"
fi

cd $buildpath
unzip $buildpath/gatk-4.0.0.0.zip && rm -rf $buildpath/gatk-4.0.0.zip
mkdir -p $installpath/gatk-4.0.0.0 && cp -r $buildpath/gatk-4.0.0.0/* $installpath/gatk-4.0.0.0

mkdir -p $installpath/gatk-4.0.0.0/com/intel/gkl/
cp -r $installpath/gkl-0.8.10/build/classes/java/main/com/intel/gkl/compression $installpath/gatk-4.0.0.0/com/intel/gkl/
cp -r $installpath/gkl-0.8.10/build/classes/java/main/com/intel/gkl/IntelGKLUtils.class $installpath/gatk-4.0.0.0/com/intel/gkl/
cp -r $installpath/gkl-0.8.10/build/classes/java/main/com/intel/gkl/native/ $installpath/gatk-4.0.0.0/com/intel/gkl/
cp -r $installpath/gkl-0.8.10/build/classes/java/main/com/intel/gkl/pairhmm/ $installpath/gatk-4.0.0.0/com/intel/gkl/
cp -r $installpath/gkl-0.8.10/build/classes/java/main/com/intel/gkl/smithwaterman/ $installpath/gatk-4.0.0.0/com/intel/gkl/
cp -r $installpath/gkl-0.8.10/build/native/libgkl* $installpath/gatk-4.0.0.0/com/intel/gkl/
jar uvf $installpath/gatk-4.0.0.0/gatk-package-4.0.0.0-spark.jar $installpath/gatk-4.0.0.0/com/intel/gkl/*
jar uvf $installpath/gatk-4.0.0.0/gatk-package-4.0.0.0-local.jar $installpath/gatk-4.0.0.0/com/intel/gkl/*

cat>"$installpath/gatk-4.0.0.0/gatk_modulefiles"<<EOF
#%Module1.0
conflict gatk
variable modfile [file normalize [info script]]
proc getModulefileDir {} {
    variable modfile
    set modfile_path [file dirname \$modfile]
    return \$modfile_path
}
set pwd [getModulefileDir]
set GATK \$pwd
setenv GATK \$GATK
prepend-path PATH \$GATK
EOF

rm -rf $buildpath/gatk-4.0.0.0
